package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.chefduel.ChefDuelModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CHEF_DUEL2_CLOSE_SERVE_DISH_ONE_KEY, desc = "收摊")
public class ChefDuel2CloseTableOneKeyCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ChefDuelProto.ChefDuel2CloseTableOneKeyReqMsg reqMsg = ChefDuelProto.ChefDuel2CloseTableOneKeyReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        ActivityInfo activityInfo = ChefDuelActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!ChefDuelActivityMgr.isActivityMatchTime(activityInfo)){
            ret = GameErrorCode.E_CHEF_DUEL_NOT_IN_MATCH_TIME;
            return;
        }
        if(!player.getModule(ChefDuelModule.class).isAble(activityId)){
            ret = GameErrorCode.E_CHEF_DUEL2_USER_NOT_ABLE;
            return;
        }
        if(!ActivityMgr.activityInTime(activityInfo)){
            ret = GameErrorCode.E_CHEF_DUEL_NOT_IN_MATCH_TIME;
            return;
        }
        player.sendPacket(Protocol.C_CHEF_DUEL2_CLOSE_SERVE_DISH_ONE_KEY, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            ChefDuelProto.ChefDuel2CloseTableRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2CloseTableRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_CHEF_DUEL2_CLOSE_SERVE_DISH_ONE_KEY, respMsg);
        }
    }
}
